package com.freesun.shop.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.freesun.shop.entity.ProdProp;
import com.freesun.shop.entity.ProdPropValue;
import com.freesun.shop.service.ProdPropService;
import com.freesun.shop.service.ProdPropValueService;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/prod/spec")
public class ProdPropController {

    @Reference(check = false)
    private ProdPropService prodPropService;
    @Reference(check = false)
    private ProdPropValueService prodPropValueService;

    @ApiOperation("商品规格分页条件查询")
    @GetMapping("/page")
    public ResponseEntity<IPage<ProdProp>> page(ProdProp prodProp, Page<ProdProp> page){
        IPage<ProdProp> iPage= prodPropService.findByPage(prodProp,page);
        return ResponseEntity.ok(iPage);
    }
    @ApiOperation("删除商品规格")
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteProdProp(@PathVariable("id")Long id){

        prodPropService.removeById(id);
        return ResponseEntity.ok().build();
    }
    @ApiOperation("新增商品属性规格")
    @PostMapping
    public ResponseEntity<Void> save(@RequestBody ProdProp prodProp){
        prodPropService.save(prodProp);
        return ResponseEntity.ok().build();
    }

    @ApiOperation("修改商品属性规格")
    @PutMapping
    public ResponseEntity<Void> updateById(@RequestBody ProdProp prodProp){
        prodPropService.updateById(prodProp);
        return ResponseEntity.ok().build();
    }
    @ApiOperation("商品规格查询")
    @GetMapping("/list")
    public ResponseEntity<List<ProdProp>> list(){
        List<ProdProp> prodProps = prodPropService.list();
        return ResponseEntity.ok(prodProps);
    }

    @ApiOperation("通过属性的ID,查询数据的值")
    @GetMapping("/listSpecValue/{id}")
    public ResponseEntity<List<ProdPropValue>> listSpecValue(@PathVariable("id") Long id){
        List<ProdPropValue> prodPropValues = prodPropValueService.listProdById(id);
        return ResponseEntity.ok(prodPropValues);
    }
}
